{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import numpy as np\n",
    "import mdtraj as md\n",
    "np.set_printoptions(threshold=50)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sometimes your molecular dynamics trajectory files are too large to\n",
    "fit into memory. This can make analysis a burden, because you have to\n",
    "be very aware of the size of various objects. This can be a challenge\n",
    "in python because of the language's automatic memory management.\n",
    "\n",
    "Fortunately, python provides the iterator protocol that can help\n",
    "us out here. We can \"stream through\" a trajectory, without loading\n",
    "the entire thing into memory at all. Instead, we'll process it in\n",
    "chunks.\n",
    "\n",
    "For the purpose of this example, we'll use a short trajectory\n",
    "that's included with MDTraj for testing purposes. When you use this\n",
    "recipe yourself, you probably will want to point your code to your\n",
    "own trajectory file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "traj_filename = 'data/frame0.h5'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, if you only want a single frame of a trajectory, there's no reason\n",
    "to load up the whole thing. `md.load_frame` can load up a single\n",
    "frame for you. Let's get the first one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "first_frame = md.load_frame(traj_filename, 0)\n",
    "first_frame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using `md.iterload`, you can iterate through chunks\n",
    "of the trajectory. If you don't retain a reference to the chunk\n",
    "as you iterate through, then the python garbage collector can recycle\n",
    "the memory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rmsds = []\n",
    "for chunk in md.iterload(traj_filename, chunk=100):\n",
    "    rmsds.append(md.rmsd(chunk, first_frame))\n",
    "    print(chunk, '\\n', chunk.time)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, we've calculated all of the rmsds chunk by chunk, and we\n",
    "can take a look at them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rmsds = np.concatenate(rmsds)\n",
    "\n",
    "print(rmsds)\n",
    "print('max rmsd ', np.max(rmsds), 'at index', np.argmax(rmsds))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
